//
//  CategoryTableViewCell.m
//  zhijian
//
//  Created by lcb on 2019/4/5.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "CategoryTableViewCell.h"

@interface CategoryTableViewCell ()

@property (strong, nonatomic) UILabel *lblKey;
@property (strong, nonatomic) UIView *keyView;
@property (assign, nonatomic) BOOL isAll;
@property (strong, nonatomic) NSMutableArray *kindlist;
@end
@implementation CategoryTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setup];
        self.isAll=NO;
        if (!_kindlist) {
            _kindlist=[NSMutableArray array];
        }
    }
    return self;
}
-(void)setup{
    _lblKey = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 20)];
    _lblKey.font = [UIFont systemFontOfSize:18];
    _lblKey.textColor=UIColorOfHex(0x333333);
    _lblKey.text =@"分类";
    [self addSubview:_lblKey];
    //
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(_lblKey.frame.origin.x+_lblKey.frame.size.width, 15, 60, 10);
    [btn setTitle:@"全选" forState:UIControlStateNormal];
    btn.titleLabel.font =[UIFont systemFontOfSize:12];
    [btn setTitleColor:navColor forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btn addTarget:self action:@selector(selectedAllAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    //
    self.keyView = [[UIView alloc] initWithFrame:CGRectMake(20, 50, SCREEN_WIDTH-40, 200)];
    self.keyView.backgroundColor=[UIColor clearColor];
    [self addSubview:self.keyView];
    [self reloadkeyView];
}
-(void)setType:(NSInteger)type{
    _type =type;
    [self reloadkeyView];
}
-(void)reloadkeyView
{
    for (UIView *subView in [self.keyView subviews]) {
        [subView removeFromSuperview];
    }
    NSArray *keyArray = [[BaseShare sharedBaseShare] getUser].basicOumeng;
    if (self.type==2) {
        keyArray =[[BaseShare sharedBaseShare] getUser].basicHan;
    }
    NSInteger column = 3;  //列数
    NSInteger buttonCount = keyArray.count;//按钮个数
    //按钮的宽高
    CGFloat buttonW = self.keyView.frame.size.width / column;
    CGFloat buttonH = 40;
    for (int i = 0; i < buttonCount; i++) {
        NSDictionary *han =[keyArray objectAtIndex:i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(((i % column) * buttonW), ((i / column) * buttonH), buttonW - 10, buttonH - 10);
        btn.tag = [[han objectForKey:@"id"] integerValue];
        btn.titleLabel.font =[UIFont systemFontOfSize:14];
        [btn setTitle:[han objectForKey:@"name"] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorOfHex(0x000000) forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"cate_nor"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"cate_sel"] forState:UIControlStateSelected];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        btn.selected =self.isAll;
        if (self.isAll) {
            NSString *tagstr =[NSString stringWithFormat:@"%ld",btn.tag];
            [self.kindlist addObject:tagstr];
        }
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.keyView addSubview:btn];
    }
}
- (void)btnAction:(UIButton *)sender{
    NSLog(@"this is current selected indexs: %ld",sender.tag);
    sender.selected = !sender.selected;
    NSMutableArray *tags =[self kindArray:sender.tag isSelected:sender.selected];
    NSString *tagstr = @"";
    for (int i=0; i<tags.count; i++) {
        NSString *ttags = [tags objectAtIndex:i];
        if (i==0) {
            tagstr=[tagstr stringByAppendingFormat:@"%@", ttags];
        }else{
             tagstr=[tagstr stringByAppendingFormat:@",%@", ttags];
        }
    }
    self.isAll=NO;
    if(_cateSelectedBlock)
    {
        if (self.isAll) {
             _cateSelectedBlock(@"");
        }else{
            _cateSelectedBlock(tagstr);
        }
       
    }
}
-(void)selectedAllAction:(UIButton *)sender{
    self.isAll=!self.isAll;
    [self.kindlist removeAllObjects];
    [self reloadkeyView];
    
    NSString *tagstr = @"";
    for (int i=0; i<self.kindlist.count; i++) {
        NSString *ttags = [self.kindlist objectAtIndex:i];
        if (i==0) {
            tagstr=[tagstr stringByAppendingFormat:@"%@", ttags];
        }else{
            tagstr=[tagstr stringByAppendingFormat:@",%@", ttags];
        }
    }
    if(_cateSelectedBlock)
    {
        if (self.isAll) {
            _cateSelectedBlock(@"");
        }else{
            _cateSelectedBlock(tagstr);
        }
        
    }
}
-(NSMutableArray *)kindArray:(NSInteger)tag isSelected:(BOOL)isSelect{
    NSString *tagstr =[NSString stringWithFormat:@"%ld",tag];
   
    if (isSelect) {
        if (![self.kindlist containsObject:tagstr]) {
            [self.kindlist addObject:tagstr];
        }
    }else{
        [self.kindlist removeObject:tagstr];
    }
    return self.kindlist;
}
@end
